<?php
namespace app\api\controller;
use app\common\controller\ApiBase;
use app\common\library\ErrorType;
use app\common\library\Sign;
use app\common\library\YuUtil;
use app\common\model\UrlPerson;
use app\common\model\User;
use think\Request;


/**
 * @title 密码管理类
 * @description 密码管理类
 * @package app\api\controller
 */
class Passadmin extends ApiBase
{
    /**
     * @var 用户登陆的id
     */
    protected $uid;
    /**
     * @var array 前置操作，这里是检查登录
     */
    protected $beforeActionList = [
        'checkAuth'
    ];

    /**
     * @title 检查登录
     * @description 检查登录
     * @author Admin ldy
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/19  9:36
     */
    protected function checkAuth(){
        /// 如需登录，调用此方法鉴权
        /// 注解1：如果已经登录，则会获取到uid;否则会直接打印错误信息，不执行下面的代码
        /// 注解2：该方法走的是缓存，不通过数据库检查
        /// 注解3：如果没有token信息，需要调登录接口，重新获取token，并保存，token有效期暂定为30天
        /// 注解4：该类所有方法需要登录鉴权，故预先加载
        $this->uid = $this->getUidByCheck();
    }


    /**
     * @title 获取所有的用户密码
     * @description 获取所有的用户密码
     * @author Admin ldy
     * @url /api/Passadmin/getAllPas
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2019/1/3  11:25
     */
    public function getAllPas(){
        $uid = $this->uid;
        $this->checkEmptyParams($uid);
        $re = \app\common\library\YuPasswordAdmin::findPasswordByUid($uid);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 新增一条密码记录
     * @description 新增一条密码记录
     * @author Admin ldy
     * @url /api/Passadmin/addPas
     * @param name:token  type:string require:1 default:1 desc:登录身份验证
     * @param name:title  type:string require:1 default:1 desc:标题
     * @param name:account  type:string require:1 default:1 desc:账户名
     * @param name:password  type:string require:1 default:1 desc:密码
     * @param name:login_url  type:string require:0 default:1 desc:登录地址
     * @param name:sort  type:string require:0 default:1 desc:排序
     * @param name:desc  type:string require:0 default:1 desc:备注
     * @time 2019/1/3  11:32
     */
    public function addPas(){
        $uid = $this->uid;
        $title = $this->request->param('title');
        $account = $this->request->param('account');
        $password = $this->request->param('password');
        $login_url = $this->request->param('login_url');
        $sort = $this->request->param('sort');
        $desc = $this->request->param('desc');
        $this->checkEmptyParams($uid);
        $this->checkEmptyParams($title);
        $this->checkEmptyParams($account);
        $this->checkEmptyParams($password);
        $option=[
            'title'=>$title,
            'account'=>$account,
            'password'=>$password,
            'login_url'=>$login_url,
            'sort'=>$sort,
            'desc'=>$desc
        ];
        $re = \app\common\library\YuPasswordAdmin::addPassword($uid,$option);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 修改一条密码记录
     * @description 修改一条密码记录
     * @author Admin ldy
     * @url /api/Passadmin/updatePas
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:id  type:string require:1 default:1 desc: 记录的id
     * @param name:title  type:string require:1 default:1 desc: 标题
     * @param name:account  type:string require:1 default:1 desc: 账户名
     * @param name:password  type:string require:1 default:1 desc: 密码
     * @param name:login_url  type:string require:1 default:1 desc: 登录地址
     * @param name:sort  type:string require:1 default:1 desc: 排序
     * @param name:desc  type:string require:1 default:1 desc:备注
     * @time 2019/1/3  11:32
     */
    public function updatePas(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $account = $this->request->param('account');
        $password = $this->request->param('password');
        $login_url = $this->request->param('login_url');
        $sort = $this->request->param('sort');
        $desc = $this->request->param('desc');
        $this->checkEmptyParams($id);
        $this->checkEmptyParams($title);
        $this->checkEmptyParams($account);
        $this->checkEmptyParams($password);
        $option=[
            'title'=>$title,
            'account'=>$account,
            'password'=>$password,
            'login_url'=>$login_url,
            'sort'=>$sort,
            'desc'=>$desc
        ];
        $re = \app\common\library\YuPasswordAdmin::changePassword($id,$option);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 删除一条密码记录
     * @description 删除一条密码记录
     * @author Admin ldy
     * @url /api/Passadmin/delPas
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:id  type:string require:1 default:1 desc: 记录的id
     * @time 2019/1/3  11:32
     */
    public function delPas(){
        $id = $this->request->param('id');
        $this->checkEmptyParams($id);
        $re = \app\common\library\YuPasswordAdmin::delPassword($id);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 查看一条密码记录
     * @description 查看一条密码记录
     * @author Admin ldy
     * @url /api/Passadmin/showPas
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:id  type:string require:1 default:1 desc: 记录的id
     * @time 2019/1/3  11:32
     */
    public function showPas(){
        $id = $this->request->param('id');
        $this->checkEmptyParams($id);
        $re = \app\common\library\YuPasswordAdmin::getRealPassWord($id);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 测试
     * @description 测试
     * @author Admin ldy
     * @url /api/message/test
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/10/15  13:50
     */
    public function test(){
        //测试发送一条站内信
//        $re = \app\common\library\YuMsg::sendMsgToUserByUserId($this->uid,'测试消息1','这是一条测试消息1');
//        if ($re->getStatus()){
//            $this->success('成功！',$re->getData(),200);
//        }else{
//            $this->err($re->getMsg());
//        }
    }


}
